module AdminHelper
  def button_to_edit(name, path, class_name = nil)
    link_to "", path, :class => "edit_button #{class_name}", :title => "#{name}"
  end

  def button_to_delete(name, path, class_name = nil)
    link_to "", path, :class => "delete_button #{class_name}", :method => :delete, :title => "#{name}", :confirm => "Opravdu chcete smazat tento záznam?"
  end

  def button_to_new(name, path, class_name = nil)
    link_to "#{name}", path, :class => "new_button #{class_name}", :title => "#{name}"
  end

  def button_to_confirm(name, path, class_name = nil, method = "POST")
    link_to "", path, :class => "confirm_button #{class_name}", :title => "#{name}", :method => method.to_sym
  end

  def button_to_search(name)
    submit_tag "", :class => "search_button"
  end

  def button_to_detail(name, path, class_name = nil)
    link_to "", path, :class => "detail_button #{class_name}", :title => "#{name}"
  end

  def button_to_clone(name, path, class_name = nil)
    link_to "", path, :class => "star_button #{class_name}", :title => "#{name}"
  end

  def button_to_left(name, path, class_name = nil)
    link_to "", path, :class => "left_button #seqarch{class_name}", :title => "#{name}"
  end

  def button_to_right(name, path, class_name = nil)
    link_to "", path, :class => "right_button #{class_name}", :title => "#{name}"
  end

  def button_to_top(name, path, class_name = nil)
    link_to "", path, :class => "top_button #{class_name}", :title => "#{name}"
  end

  def button_to_bottom(name, path, class_name = nil)
    link_to "", path, :class => "bottom_button #{class_name}", :title => "#{name}"
  end

  def remote_button_to_confirm(name, path, class_name = nil)
    link_to "", path, :class => "confirm_button #{class_name}", :title => "#{name}", :remote => true
  end

  def remote_button_to_new(name, path, class_name = nil)
    link_to "#{name}", path, :class => "new_button #{class_name}", :title => "#{name}", :remote => true
  end

  def remote_button_to_edit(name, path, class_name = nil)
    link_to "", path, :class => "edit_button #{class_name}", :title => "#{name}", :remote => true
  end

  def remote_button_to_delete(name, path, class_name = nil)
    link_to "", path, :class => "delete_button #{class_name}", :title => "#{name}", :remote => true
  end

  def button_to_detail_disabled(message = "Nemáte dostatečná práva pro zobrazení detailu záznamu", class_name = nil)
    "<acronym title=\"#{message}\" class=\"detail_button_disabled #{class_name}\"></acronym>".html_safe
  end

  def button_to_edit_disabled(message = "Nemáte dostatečná práva pro editaci záznamu", class_name = nil)
    "<acronym title=\"#{message}\" class=\"edit_button_disabled #{class_name}\"></acronym>".html_safe
  end

  def button_to_new_disabled(message = "Nemáte dostatečná práva pro vytvoření záznamu", class_name = nil)
    "<acronym title=\"#{message}\" class=\"new_button_disabled #{class_name}\"></acronym>".html_safe
  end

  def button_to_delete_disabled(message = "Nemáte dostatečná práva pro smazání záznamu", class_name = nil)
    "<acronym title=\"#{message}\" class=\"delete_button_disabled #{class_name}\"></acronym>".html_safe
  end

  def button_to_confirm_disabled(message = "Nemáte dostatečná práva pro uložení záznamu", class_name = nil)
    "<acronym title=\"#{message}\" class=\"confirm_button_disabled #{class_name}\"></acronym>".html_safe
  end
  
  def spinner(id = "spinner")
    "<p style=\"display: none;\" id=\"#{id}\">Nahrávám data ... <img src=\"/assets/shared/spinner.gif\"></p>".html_safe
  end

  def link_to_remove_fields(name, f, class_to_destroy = 'fields')
    f.hidden_field(:_destroy, :class => "destroy") + link_to_function("", "remove_fields(this, '#{class_to_destroy}')", :class => "delete_button", :title => "#{name}")
  end

  def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, [new_object], :child_index => "new_#{association}") do |builder|
      render("form_" + association.to_s, :f => builder)
  end

    link_to_function("", "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")", :class => "new_button", :title => "#{name}")
  end

  def link_to_add_property_attributes(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    new_object.property_id = f.object.id
    #new_object.parent_id   = f.object.id
    new_object.save!(:validate => false)
    
    fields = f.fields_for(association, [new_object], :child_index => "new_#{association}") do |builder|
      render("new_" + association.to_s, :f => builder)
    end

    link_to_function("", "add_fields_for_property_attributes(this, \"#{association}\", \"#{escape_javascript(fields)}\")", :class => "new_button", :title => "#{name}")
  end
end
